package task

import (
	"github.com/fastknifes/usdt-pay/internal/service"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/os/gcron"
	"github.com/gogf/gf/os/gctx"
)

func init() {

}

type ListenTrade struct {
}

func (lt *ListenTrade) Start() {
	// 添加定时任务
	lt.addCronJob("* * * * * *", lt.listenPolling, "listenPolling")
	lt.addCronJob("*/15 * * * * *", lt.pushCallback, "pushCallback")
	lt.addCronJob("*/10 * * * * *", lt.deleteTimeout, "deleteTimeout")
}

// 新增方法：封装定时任务添加逻辑
func (lt *ListenTrade) addCronJob(spec string, callback func(), name string) {
	_, err := gcron.Add(spec, callback, name)
	if err != nil {
		g.Log().Fatal(gctx.New(), "添加定时任务失败:", err)
	}
}

/**
* 监听回调
**/
var tradeService = &service.TradeService{}

func (lt ListenTrade) listenPolling() {

	tradeService.Polling()
}

/**
* 推送回调
**/
func (lt ListenTrade) pushCallback() {
	tradeService.Notify()
}

func (lt ListenTrade) deleteTimeout() {
	tradeService.DeleteTimeout()
}
